

#include "armiesscene.h"
#include "resourcemanager.h"
#include "log.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "gamescene.h"
#include "formationscene.h"
#include "CCSkeletonCache.h"
#include "campaignscene.h"
#include "collectionscene.h"
#include "shopscene.h"
#include "battlescene.h"
#include "frameanimation.h"
#include "menuscene.h"
#include "goldcoinscene.h"

ArmiesScene::ArmiesScene():
_layer(nullptr)
, _initialized(false)
{
	memset(_formationPreviews, 0, sizeof(_formationPreviews));
}

ArmiesScene::~ArmiesScene() {
	for(int i = 0; i < FORMATION_COUNT; i++) {
		_formationPreviews[i]->removeFromParent();
	}

	for(int i = 0; i < FORMATION_COUNT; i++) {
		sprintf(buf, "%sformation_%d_preview.jpg", CCFileUtils::sharedFileUtils()->getWritablePath().c_str(), i);
		if(CCFileUtils::sharedFileUtils()->isFileExist(buf)) {
			CCTextureCache::sharedTextureCache()->removeTextureForKey(buf);
		}
	}
}

bool ArmiesScene::init() {
	if (!CCScene::init()) {
		return false;
	}

	_layer = CCLayer::create();
	addChild(_layer);

	CCSprite *spr = CCSprite::create("textures/ui_bg.png");
	spr->setAnchorPoint(CCPointZero);

	CCSize size = spr->getContentSize();
	spr->setScaleX(GM->screenSize.width / size.width);
	spr->setScaleY(GM->screenSize.height / size.height);
	_layer->addChild(spr);

	CCSprite* sprNavigatorBar = MAKE_SPRITE("navigator_bar.png");
	sprNavigatorBar->setPosition(XY_SCALE(0.0f, 536));
	sprNavigatorBar->setScaleX(GM->xscale);
	sprNavigatorBar->setAnchorPoint(ccp(0.0f, 0.5f));
	_layer->addChild(sprNavigatorBar);

	auto btnBack = RM->makeBackButton(this, menu_selector(ArmiesScene::onBack), _layer, false);
	btnBack->setPosition(XY_SCALE(72,540));

	auto btnMoney = RM->makeButton("button/money_button_0.png", "button/money_button_0.png", nullptr, this, menu_selector(ArmiesScene::onRecharge), _layer);
	btnMoney->setPosition(XY_SCALE(913,540));

	auto sprGoldIcon = MAKE_SPRITE("gold_icon.png");
	sprGoldIcon->setPosition(XY_SCALE(755, 540));
	_layer->addChild(sprGoldIcon);

	sprintf(buf, "%d", GDM->money);
	_labelMoney = CCLabelTTF::create(buf, "", 24);
	_labelMoney->setPosition(XY_SCALE(785, 540));
	_labelMoney->setColor(ccc3(237, 199, 143));
	_labelMoney->setAnchorPoint(ccp(0.0f, 0.5f));
	_layer->addChild(_labelMoney);

	int button_x_pos[] = {167,264,366,476,572,678};
	const char* button_names_1[] = {
			"button/navigator_button_campaign_0.png",
			"button/navigator_button_battle_0.png",
			"button/navigator_button_army_0.png",
			"button/navigator_button_collection_0.png",
			"button/navigator_button_shop_0.png",
			"button/navigator_button_flag_0.png",};

	const char* button_names_0[] = {
			"button/navigator_button_campaign_1.png",
			"button/navigator_button_battle_1.png",
			"button/navigator_button_army_1.png",
			"button/navigator_button_collection_1.png",
			"button/navigator_button_shop_1.png",
			"button/navigator_button_flag_1.png",};
	
	CCMenuItemSprite* buttons[6];

	for(int i = 0; i < 6; i++) {
		auto btn =  RM->makeButton(button_names_0[i], button_names_1[i], nullptr,	this, nullptr, _layer);
		btn->setPosition(XY_SCALE(button_x_pos[i], 540));
		buttons[i] = btn;
	}

	buttons[0]->setTarget(this, menu_selector(ArmiesScene::onPlayCampaign));
	buttons[1]->setTarget(this, menu_selector(ArmiesScene::onStartBattle));
	buttons[3]->setTarget(this, menu_selector(ArmiesScene::onViewCollection));
	buttons[4]->setTarget(this, menu_selector(ArmiesScene::onOpenShop));
	buttons[5]->setTarget(this, menu_selector(ArmiesScene::onEditFlag));

	CCPoint ARR_FORMATION_PREVIEW_POS[] = {
			CCPoint(255, 340),
			CCPoint(683, 340),
			CCPoint(255, 152),
			CCPoint(683, 152),
	};

	for(int i = 0; i < FORMATION_COUNT; i++) {
		sprintf(buf, "%sformation_%d_preview.jpg", CCFileUtils::sharedFileUtils()->getWritablePath().c_str(), i);
		if(!CCFileUtils::sharedFileUtils()->isFileExist(buf)) {
			_DHLOG("not found preview:%s", buf);
			sprintf(buf, "textures/blank_formation.png");
		}

		auto btn =  RM->makeButton(buf, nullptr, nullptr,	this, menu_selector(ArmiesScene::onEditFormation), _layer);
		CCSize btnSize = btn->getContentSize();
		btn->setPosition(SCALE_PT(ARR_FORMATION_PREVIEW_POS[i]));
		btn->setScaleX(350.0f / btnSize.width);
		btn->setScaleY(145.0f / btnSize.height);
		btn->setTag(i * 100 + 100);
		_formationPreviews[i] = btn;

		sprintf(buf, "%d", i + 1);
		CCLabelTTF* label = CCLabelTTF::create(buf, "", 96);
		label->setColor(ccc3(89,3,0));
		label->setPosition(ccp(btn->getContentSize().width / 2, btn->getContentSize().height / 2));
		btn->addChild(label);
	}

	auto btnTest =  RM->makeButton("button/blue_button_0.png", "button/blue_button_0.png", nullptr, this, menu_selector(ArmiesScene::onTestBattle), _layer);
	btnTest->setPosition(XY_SCALE(850, 450));

	auto btnTest2 =  RM->makeButton("button/blue_button_0.png", "button/blue_button_0.png", nullptr, this, menu_selector(ArmiesScene::onTestBattle2), _layer);
	btnTest2->setPosition(XY_SCALE(850, 400));

	_initialized = true;

	return true;
}

void ArmiesScene::update(float dt) {
}

void ArmiesScene::onStartBattle(CCObject *sender) {
	auto scene = BattleScene::create();
	CCDirector::sharedDirector()->replaceScene(scene);
}

void ArmiesScene::onTestBattle(CCObject *sender) {
	auto scene = GameScene::create();
	scene->setFormations(GDM->formations[0], GDM->formations[1]);
	CCDirector::sharedDirector()->replaceScene(scene);
}

void ArmiesScene::onTestBattle2(CCObject *sender) {
	auto scene = GameScene::create();
	scene->setFormations(GDM->formations[2], GDM->formations[3]);
	CCDirector::sharedDirector()->replaceScene(scene);
}

void ArmiesScene::onBack(CCObject *sender) {
	CCDirector::sharedDirector()->replaceScene(MenuScene::create());
}

void ArmiesScene::onEditFormation(CCObject *sender) {
	CCNode* node = (CCNode*)sender;
	int index = node->getTag() / 100 - 1;

	auto scene = FormationScene::create(index);
	CCDirector::sharedDirector()->replaceScene(scene);

}

void ArmiesScene::onPlayCampaign(CCObject *sender) {
	CampaignScene* scene = CampaignScene::create();
	CCDirector::sharedDirector()->replaceScene(scene);
}

void ArmiesScene::onViewCollection(CCObject *sender) {
	CollectionScene* scene = CollectionScene::create();
	CCDirector::sharedDirector()->replaceScene(scene);
}

void ArmiesScene::onOpenShop(CCObject *sender) {
	ShopScene* scene = ShopScene::create();
	CCDirector::sharedDirector()->replaceScene(scene);
}

void ArmiesScene::onEditFlag(CCObject *sender) {

}

void ArmiesScene::keyBackClicked() {
	onBack(nullptr);
}

void ArmiesScene::keyMenuClicked() {

}

void ArmiesScene::onEnter() {
	CCNode::onEnter();

	CCDirector::sharedDirector()->getKeypadDispatcher()->addDelegate(this);

	if(_initialized) {
		updateFormationPreviews();
	}
}

void ArmiesScene::onExit() {
	CCNode::onExit();

	CCDirector::sharedDirector()->getKeypadDispatcher()->removeDelegate(this);
}

void ArmiesScene::updateFormationPreviews() {
	for(int i = 0; i < FORMATION_COUNT; i++) {
		_formationPreviews[i]->setNormalImage(CCSprite::create("textures/blank_formation.png"));

		sprintf(buf, "%sformation_%d_preview.jpg", CCFileUtils::sharedFileUtils()->getWritablePath().c_str(), i);
		if(CCFileUtils::sharedFileUtils()->isFileExist(buf)) {
			CCTextureCache::sharedTextureCache()->removeTextureForKey(buf);
			_formationPreviews[i]->setNormalImage(CCSprite::create(buf));
		}

		CCSize btnSize = _formationPreviews[i]->getContentSize();

		_formationPreviews[i]->setScaleX(350.0f / btnSize.width);
		_formationPreviews[i]->setScaleY(145.0f / btnSize.height);

		sprintf(buf, "%d", i + 1);
		CCLabelTTF* label = CCLabelTTF::create(buf, "", 96);
		label->setColor(ccc3(89,3,0));
		label->setPosition(ccp(btnSize.width / 2, btnSize.height / 2));
		_formationPreviews[i]->addChild(label);
	}
}

void ArmiesScene::onRecharge(CCObject *sender) {
	GoldCoinScene* scene = GoldCoinScene::create();
	CCDirector::sharedDirector()->pushScene(scene);
}